package com.campusrecruitment.controller;

import com.campusrecruitment.common.JsonUtils;
import com.campusrecruitment.common.R;
import com.campusrecruitment.pojo.employer;
import com.campusrecruitment.pojo.loginUser;
import com.campusrecruitment.pojo.resume;
import com.campusrecruitment.pojo.student;
import com.campusrecruitment.service.employerService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.List;

/**
 * 单位信息管理
 */
@Slf4j
@RestController
@RequestMapping("/employer")
public class employerController {
    private ObjectMapper mapper = new ObjectMapper();


    /**
     * 通过名字查询单位
     * @param employer
     * @return
     */
    @PostMapping
    public R<employer> getByName(@RequestBody employer employer) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\employer.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<employer> employers= mapper.readValue(jsonList,new TypeReference<List<employer>>(){});

        String name=employer.getName();
        for (employer e:employers
             ) {
            if (e.getName().equals(name)){
                return R.success(e);
            }
        }
        return R.error("不存在该用户");
    }

    /**
     * 查询全部
     * @return
     */
    @GetMapping("/list")
    public R<List<employer>> list() throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\employer.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<employer> employers= mapper.readValue(jsonList,new TypeReference<List<employer>>(){});
        return R.success(employers);
    }

    /**
     * 查询登录单位信息
     * @return
     */
    @PostMapping("/init")
    public R<employer> init(@RequestBody employer employer) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils = new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\employer.json"));
        String jsonList = jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<employer> employers = mapper.readValue(jsonList, new TypeReference<List<employer>>() {
        });

        String name =employer.getName();
        for (employer s : employers
        ) {
            if (s.getName().equals(name)) {
                return R.success(s);
            }
        }
        return R.error("找不到该用户");
    }

    /**
     * 修改登录用户信息
     * @param
     */
    @PostMapping("/update")
    public R<employer> update(@RequestBody employer employer) throws JsonProcessingException {
        //初始化json工具
        JsonUtils jsonUtils=new JsonUtils(new File("D:\\IDEA\\CampusRecruitment\\src\\main\\resources\\Data\\employer.json"));
        String jsonList=jsonUtils.readJson(String.class);
        //得到单位信息集合
        List<employer> employers= mapper.readValue(jsonList,new TypeReference<List<employer>>(){});

        String name=employer.getName();

        for (int i=0;i<employers.size();i++
        ) {
            if (employers.get(i).getName().equals(name)){
                //更新该用户
                employers.get(i).setId(employer.getId());
                employers.get(i).setName(employer.getName());
                employers.get(i).setCity(employer.getCity());
                employers.get(i).setAddress(employer.getAddress());
                employers.get(i).setSize(employer.getSize());
                employers.get(i).setPhone(employer.getPhone());
                break;
            }
        }
        String jsonString = mapper.writerWithDefaultPrettyPrinter()
                .writeValueAsString(employers);
        jsonUtils.writeJson(jsonString);
        return R.success(employer);
    }
}
